#!/usr/bin/env node
import { Command } from 'commander';
import { getApiDoc } from './api-get.js';
import { genApiCode } from './api-gen.js';
import module from 'module'

var require = module.createRequire(import.meta.url)
var packageJson = require('../package.json')
var projectName = packageJson.name

var program = new Command()

var fetchCommand = new Command('fetch')
  .arguments('<url>')
  .option('-d, --doc <doc>', 'output api doc json file', './api-doc.json')
  .description(`fetch api docs from swagger ui, eg: ${projectName} fetch http://localhost:8083/v1/v2/api-docs`)
  .action((args, options, command) => {
    var url = command.args[0];

    getApiDoc(url, options.doc);
  })

var genCommand = new Command('gen')
  .description(`gen dart code from api docs, eg: ${projectName} gen -c ./config.js -o ./lib/controllers`)
  .option('-c, --config <config>', 'javascript config file')
  .option('-o, --output <output>', 'output dir to gen code', './lib/controllers')
  .option('-d, --doc <doc>', 'api doc json file', './api-doc.json')
  .option('-h, --host <host>', 'api host', '')
  .action((options) => {
    genApiCode(options)
  })


  program.version(packageJson.version)
  .addCommand(fetchCommand)
  .addCommand(genCommand)
  .parse(process.argv);